package com.jeesite.modules.dataScreen.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class EmergenciesUtils {

    //突发事件excel文件相对路径
    private final static String FILE_PATH = "web-api/src/main/resources/static/file/emergenciesData.xlsx";

    private final static String ACCIDENT_TRAFFIC = "事故灾难";
    private final static String ADDRESS_DISASTERS = "地质灾害";
    private final static String MAINTENANCE_CONSTRUCTION = "养护施工";
    private final static String CROWDED = "收费站拥堵";
    private final static String SOCIAL_SECURITY = "社会安全事件";
    private final static String EXTREME_WEATHER = "气象灾害";
    private final static String OTHER_EVENTS = "其他事件";
    private final static String[] TYPES = {ACCIDENT_TRAFFIC, ADDRESS_DISASTERS, MAINTENANCE_CONSTRUCTION, CROWDED, SOCIAL_SECURITY, EXTREME_WEATHER, OTHER_EVENTS};

    //事件类型
    private static final Map<String, Set<String>> emergenciesTypes;

    //初始化突发事件类型
    static {
        emergenciesTypes = new HashMap<>();
        emergenciesTypes.put(ACCIDENT_TRAFFIC, new HashSet<>());
        emergenciesTypes.put(ADDRESS_DISASTERS, new HashSet<>());
        emergenciesTypes.put(MAINTENANCE_CONSTRUCTION, new HashSet<>());
        emergenciesTypes.put(EXTREME_WEATHER, new HashSet<>());
        emergenciesTypes.put(SOCIAL_SECURITY, new HashSet<>());
        emergenciesTypes.put(CROWDED, new HashSet<>());
        emergenciesTypes.put(OTHER_EVENTS, new HashSet<>());
        iniData();
    }

    private EmergenciesUtils() {
    }

    private static void iniData() {
        try (InputStream fileInputStream = Files.newInputStream(Paths.get(FILE_PATH));
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            //用于判断是否是获取的表头
            boolean flag = false;

            //获取每一行
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                String emergenciesType = "";
                String emergenciesName = "";

                int cellIndex = 0;
                //获取每一行的每一列的数据cellIndex就是列下标
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    if (cellIndex == 0) {
                        emergenciesName = cell.getStringCellValue();
                    } else {
                        emergenciesType = cell.getStringCellValue();
                    }
                    cellIndex++;
                }
                //第一次是获取的表头就不用添加
                if (flag && !emergenciesType.isEmpty() && !emergenciesName.isEmpty()) {
                    emergenciesTypes.get(emergenciesType).add(emergenciesName);
                }
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<Map<String, Object>> sortEmergencies(List<Map<String, Object>> data) {
        int[] count = new int[7];
        List<Map<String, Object>> ans = new LinkedList<>();
        for (Map<String, Object> emergencies : data) {
            //获取当日发生起数
            Long cnt = (Long) emergencies.get("COUNT");
            //获取事件名字
            String emergenciesName = (String) emergencies.get("TYPE_NAME");
            //获取事件类型
            String emergenciesType = getEmergenciesType(emergenciesName);
            //获取对应下标
            int index = findIndex(emergenciesType);
            //加
            count[index] += cnt;
        }
        for (int i = 0; i < 7; i++) {
            ans.add(getEmergenciesMap(TYPES[i], count[i]));
        }
        return ans;
    }

    private static String getEmergenciesType(String emergenciesName) {
        //获取所有突发事件的entrySet;
        Set<Map.Entry<String, Set<String>>> entries = emergenciesTypes.entrySet();
        //遍历
        for (Map.Entry<String, Set<String>> keyAndValue : entries) {
            //获取Set集合
            Set<String> value = keyAndValue.getValue();
            //如果传入的事件名字在set中，就返回对应的事件类型
            if (value.contains(emergenciesName)) {
                return keyAndValue.getKey();
            }
        }
        return "";
    }

    private static Map<String, Object> getEmergenciesMap(String name, Integer value) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("unit", "起");
        map.put("value", value);
        return map;
    }

    private static int findIndex(String name) {
        for (int i = 0; i < TYPES.length; i++) {
            if (TYPES[i].equals(name)) {
                return i;
            }
        }
        return 0;
    }
}
